package com.acme.domain.account;

import com.acme.domain.client.Client;
import com.acme.exceptions.NoEnoughFundsException;
import com.acme.exceptions.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount {

	private double overdraft;

	public CheckingAccount(final int id, final double amount,
			final double overdraft) {
		super(id, amount);
		this.overdraft = overdraft;
	}

	@Override
	public void withdraw(final double amount) throws NoEnoughFundsException {
		if (amount < 0) {
			throw new IllegalArgumentException("Amount can not be negative");
		}
		if (this.balance - amount >= -overdraft) {
			this.balance -= amount;
		} else {
			throw new NoEnoughFundsException(amount);
		}

		if (this.balance < amount) {
			// Not enough balance to cover the amount requested to withdraw
			// Check if there is enough in the overdraft protection (if any)
			double overdraftNeeded = amount - this.balance;
			if (overdraft < overdraftNeeded) {
				throw new OverDraftLimitExceededException(overdraftNeeded
						- overdraft);
			} else {
				// Yes, there is overdraft protection and enough to cover the
				// amount
				this.balance = 0.0;
				overdraft -= overdraftNeeded;
			}
		} else {
			// Yes, there is enough balance to cover the amount
			// Proceed as usual
			this.balance = this.balance - amount;
		}
	}

	@Override
	public void deposit(final double amount) {
		if (amount > 0) {
			this.balance += amount;
		} else {
			throw new IllegalArgumentException("amount ca not be negative");
		}
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("Checking Account #" + this.id + " ");
		sb.append("balance: " + this.getBalance());
		sb.append(" overdraft: " + this.overdraft);
		return sb.toString();
	}
	

	public AccountType getAccountType() {
		return AccountType.CHECKING;
	}

	public double getOverdraft() {
		return overdraft;
	}
	
	@Override
	
	// TODO: [VY] Check the sinature of method at java.lang.Object and fix it
	public boolean Equals(Object obj) {
		if(obj instanceof CheckingAccount) {
			CheckingAccount chAcc = (CheckingAccount)obj;
			return (chAcc.id == this.id && chAcc.balance == this.balance && chAcc.getOverdraft() == this.overdraft);
		}
		return false;
	}
	
	public int hashCode() {
		return ((Integer)this.id).hashCode();
	}
}
